


package jnpf.zc.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.exception.DataException;
import jnpf.permission.entity.UserEntity;
import jnpf.util.GeneraterSwapUtil;
import jnpf.util.JsonUtil;
import jnpf.util.RandomUtil;
import jnpf.util.UserProvider;
import jnpf.zc.entity.ZcglZcczEntity;
import jnpf.zc.model.zcglzccz.ZcglZcczForm;
import jnpf.zc.model.zcglzccz.ZcglZcczInfoVO;
import jnpf.zc.model.zcglzccz.ZcglZcczListVO;
import jnpf.zc.model.zcglzccz.ZcglZcczPagination;
import jnpf.zc.service.ZcglZcczService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;


/**
 * 资产处置
 *
 * @版本： V3.2.0
 * @版权： 智果云科技（https://www.jnpfsoft.com）
 * @作者： JNPF开发平台组
 * @日期： 2024-12-19
 */
@Slf4j
@RestController
@Api(tags = "资产处置", value = "example")
@RequestMapping("/api/example/ZcglZccz")
public class ZcglZcczController {

    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;


    @Autowired
    private UserProvider userProvider;


    @Autowired
    private ZcglZcczService zcglZcczService;


    /**
     * 列表
     *
     * @param zcglZcczPagination
     * @return
     */
    @ApiOperation("获取列表")
    @PostMapping("/getList")
    public ActionResult list(@RequestBody ZcglZcczPagination zcglZcczPagination) throws IOException {
        List<ZcglZcczEntity> list = zcglZcczService.getList(zcglZcczPagination);
        //处理id字段转名称，若无需转或者为空可删除

        List<ZcglZcczListVO> listVO = JsonUtil.getJsonToList(list, ZcglZcczListVO.class);
        for (ZcglZcczListVO zcglZcczVO : listVO) {
            zcglZcczVO.setDisposalTitle_name(zcglZcczVO.getDisposalTitle());

            zcglZcczVO.setDisposalMethod_name(generaterSwapUtil.getDicName(zcglZcczVO.getDisposalMethod(), "511111111111111111"));


            zcglZcczVO.setHandledBy_name(zcglZcczVO.getHandledBy());

            zcglZcczVO.setDisposalUnit_name(generaterSwapUtil.getDicName(zcglZcczVO.getDisposalUnit(), "511111111111111112"));

            zcglZcczVO.setDisposalInstructions_name(zcglZcczVO.getDisposalInstructions());

            zcglZcczVO.setReasonDisposal_name(zcglZcczVO.getReasonDisposal());

            //子表数据转换
        }

        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = JsonUtil.getJsonToBean(zcglZcczPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);

    }


    @PostMapping("/addZcczgc")
    @Transactional
    @ApiOperation("新增资产处置过程")
    public ActionResult addZcczgc(@RequestBody @Valid ZcglZcczForm zcglZcczForm) throws DataException {
        ActionResult actionResult = zcglZcczService.addZccz(zcglZcczForm);
        return actionResult;
    }

    @PostMapping("/completeZcczgc")
    @Transactional
    @ApiOperation("完成资产处置过程")
    public ActionResult completeZcczgc(@RequestBody @Valid ZcglZcczForm zcglZcczForm) throws DataException {
        ActionResult actionResult = zcglZcczService.completeZccz(zcglZcczForm);
        return actionResult;
    }

    @GetMapping("/queryZcczgc/{id}")
    @ApiOperation("查询资产处置过程")
    public ActionResult queryZcczgc(@PathVariable(name = "id") String id) throws DataException {
        ActionResult actionResult = zcglZcczService.queryZcczgc(id);
        return actionResult;
    }

    @PostMapping("/completeZccz")
    @ApiOperation("资产处置完成")
    public ActionResult completeZc(@RequestBody @Valid ZcglZcczForm zcglZcczForm) {
        ActionResult actionResult = zcglZcczService.completeZc(zcglZcczForm);
        return actionResult;
    }


    /**
     * 创建
     *
     * @param zcglZcczForm
     * @return
     */
    @PostMapping
    @Transactional
    @ApiOperation("创建")
    public ActionResult create(@RequestBody @Valid ZcglZcczForm zcglZcczForm) throws DataException {
        boolean b = zcglZcczService.checkForm(zcglZcczForm, 0);
        if (b) {
            return ActionResult.fail("单行输入不能重复");
        }
        String mainId = RandomUtil.uuId();

        UserInfo userInfo = userProvider.get();
        UserEntity userEntity = generaterSwapUtil.getUser(userInfo.getUserId());
        ZcglZcczEntity entity = JsonUtil.getJsonToBean(zcglZcczForm, ZcglZcczEntity.class);
        entity.setId(mainId);
        zcglZcczService.save(entity);


        return ActionResult.success("创建成功");
    }


    /**
     * 信息
     *
     * @param id
     * @return
     */
    @ApiOperation("信息")
    @GetMapping("/{id}")
    public ActionResult<ZcglZcczInfoVO> info(@PathVariable("id") String id) {
        ZcglZcczEntity entity = zcglZcczService.getInfo(id);
        ZcglZcczInfoVO vo = JsonUtil.getJsonToBean(entity, ZcglZcczInfoVO.class);

        //子表
        //副表
        return ActionResult.success(vo);
    }

    /**
     * 表单信息(详情页)
     *
     * @param id
     * @return
     */
    @ApiOperation("表单信息(详情页)")
    @GetMapping("/detail/{id}")
    public ActionResult<ZcglZcczInfoVO> detailInfo(@PathVariable("id") String id) {
        ZcglZcczEntity entity = zcglZcczService.getInfo(id);
        ZcglZcczInfoVO vo = JsonUtil.getJsonToBean(entity, ZcglZcczInfoVO.class);

        //子表数据转换

        //附表数据转换

//添加到详情表单对象中
        vo.setDisposalTitle(vo.getDisposalTitle());

        vo.setDisposalMethod(generaterSwapUtil.getDicName(vo.getDisposalMethod(), "511111111111111111"));

        vo.setStartTime(vo.getStartTime());

        vo.setHandledBy(vo.getHandledBy());

        vo.setDisposalUnit(generaterSwapUtil.getDicName(vo.getDisposalUnit(), "511111111111111112"));

        vo.setDisposalInstructions(vo.getDisposalInstructions());

        vo.setReasonDisposal(vo.getReasonDisposal());


        return ActionResult.success(vo);
    }


    /**
     * 更新
     *
     * @param id
     * @param zcglZcczForm
     * @return
     */
    @PutMapping("/{id}")
    @Transactional
    @ApiOperation("更新")
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid ZcglZcczForm zcglZcczForm) throws DataException {
        boolean b = zcglZcczService.checkForm(zcglZcczForm, 1);
        if (b) {
            return ActionResult.fail("单行输入不能重复");
        }
        UserInfo userInfo = userProvider.get();
        ZcglZcczEntity entity = zcglZcczService.getInfo(id);
        if (entity != null) {
            ZcglZcczEntity subentity = JsonUtil.getJsonToBean(zcglZcczForm, ZcglZcczEntity.class);
            boolean b1 = zcglZcczService.updateById(subentity);
            if (!b1) {
                return ActionResult.fail("当前表单原数据已被调整，请重新进入该页面编辑并提交数据");
            }
            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败，数据不存在");
        }
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        ZcglZcczEntity entity = zcglZcczService.getInfo(id);
        if (entity != null) {
            zcglZcczService.delete(entity);

        }
        return ActionResult.success("删除成功");
    }


}
